<?php
namespace app\backstage\model;

use think\Db;
use think\Model;

class BookRecom extends Model
{
    protected $table = 'rp_book_recom_daily_agent';

    /*获取单条记录*/
    public function getOne($type, $where, $field='*',$groupBy = '')
    {
        $this->getTableName($type);
        $info = Db::table($this->table)->where($where)->field($field)->group($groupBy)->findOrEmpty();
        return $info;
    }

    /*获取列表记录*/
    public function getAll($type, $where, $field='*', $orderBy=[])
    {
        $this->getTableName($type);
        $list = Db::table($this->table)->where($where)->field($field)->order($orderBy)->select();

        return $list ? $list : [];
    }

    /*获取列表记录*/
    public function getList($type, $where, $field = '*',$page = 1, $pageSize = 100, $orderBy = [], $groupBy = '')
    {
        $this->getTableName($type);
        $list = Db::table($this->table)->where($where)->field($field)
            ->page($page,$pageSize)->order($orderBy)->group($groupBy)->select();

        return $list ? $list : [];
    }

    /*获取总记录数*/
    public function getTotalNumber($type, $where, $groupBy = '')
    {
        $this->getTableName($type);
        $count = Db::table($this->table)->where($where)->group($groupBy)->count('id');
        return $count;
    }

    /*更新记录*/
    public function updateRecord($type, $where, $data)
    {
        $this->getTableName($type);
        $res = Db::table($this->table)->where($where)->update($data);
        return $res >= 0 ? true : false;
    }

    /*添加记录*/
    public function insertRecord($type, $data)
    {
        $this->getTableName($type);
        $res = Db::table($this->table)->insert($data);
        return $res > 0 ? true : false;
    }

    /*获取回收报表统计数据表名*/
    private function getTableName($type)
    {
        switch ($type)
        {
            case 1:
                $this->table = 'rp_book_recom_daily_agent';
                break;
            case 2:
                $this->table = 'rp_book_recom_daily_channel';
                break;
            default :
                $this->table = 'rp_book_recom_daily_agent';
        }
    }
}